package main

import "math"

func minimumSum(nums []int) int {
	n := len(nums)
	res := math.MaxInt32
	for i := 0; i < n; i++ {
		for j := i + 1; j < n-1; j++ {
			for k := j + 1; k < n; k++ {
				if nums[j] > nums[i] && nums[j] > nums[k] {
					res = min(res, nums[i]+nums[j]+nums[k])
				}
			}
		}
	}
	if res < math.MaxInt32 {
		return res
	}
	return -1
}

func minimumSum1(nums []int) int {
	n := len(nums)
	res := math.MaxInt32
	mn := math.MaxInt32
	left := make([]int, n)
	left[0] = 0
	for i := 1; i < n; i++ {
		left[i] = int(math.Min(float64(nums[i-1]), float64(mn)))
		mn = left[i]
	}
	right := nums[n-1]
	for i := n - 2; i > 0; i-- {
		if left[i] < nums[i] && nums[i] > right {
			res = min(res, left[i]+nums[i]+right)
		}
		right = min(right, nums[i])
	}
	if res < math.MaxInt32 {
		return res
	}
	return -1
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
